package com.xyzq.hs.test;

import com.xyzq.hs.util.ByteUtils;


public class Message {
	/**
	 * message=[messageHeader,messageBody]
	 * @return
	 */
	public static byte[] getMessage(){
		byte[] messageHeader = getMessageHeader();
		byte[] messageBody = getMessageBody();
		byte[] message=new byte[messageHeader.length+messageBody.length];
		System.arraycopy(messageHeader, 0, message, 0, messageHeader.length);
		System.arraycopy(messageBody, 0, message, messageHeader.length, messageBody.length);
		return message;
	}
	
	
	/**
	 * header=[tag,xyzq,version,size,bodyType,checkSum]
	 * @return
	 */
	public static byte[] getMessageHeader(){
		 byte[] tag="FFFF".getBytes();
		 byte[] company="XYZQ".getBytes();
		 byte[] version=ByteUtils.int2byte(1);
		 byte[] size=ByteUtils.int2byte(25);
		 byte[] bodyType=ByteUtils.short2byte((short)1);
		 byte[] checkSum=new byte[2];
		 byte[] header = new byte[tag.length+company.length+version.length+size.length+bodyType.length+checkSum.length];
		 int headerWithoutCheckSumLength=tag.length+company.length+version.length+size.length+bodyType.length;
		 byte[] headerWithoutCheckSum=new byte[headerWithoutCheckSumLength];
		 
		int destPos=0;
	    System.arraycopy(tag,0,header,destPos,tag.length);
	    
	    destPos=destPos+tag.length;
	    System.arraycopy(company,0,header,destPos,company.length);
	    
	    destPos=destPos+company.length;
	    System.arraycopy(version,0,header,destPos,version.length);
	    
	    destPos=destPos+version.length;
	    System.arraycopy(size,0,header,destPos,size.length);
	    
	    destPos=destPos+size.length;
	    System.arraycopy(bodyType,0,header,destPos,bodyType.length);
	    
	    destPos=destPos+bodyType.length;
	    //calculate checksum 
	    System.arraycopy(header, 0, headerWithoutCheckSum, 0, headerWithoutCheckSumLength);
	    checkSum = ByteUtils.calcCheckSum(headerWithoutCheckSum);
	    System.arraycopy(checkSum,0,header,destPos,checkSum.length);
	    
		 ByteUtils.println("tag",tag);
		 ByteUtils.println("company",company);
		 ByteUtils.println("version",version);
		 ByteUtils.println("size",size);
		 ByteUtils.println("bodyType",bodyType);
		 ByteUtils.println("checkSum",checkSum);
		 ByteUtils.println("header",header);
		 return header;
	}
	
	
	/**
	 * body=[count,systemNo,functionNo,paramsLen,params]
	 * @return
	 */
	public static byte[] getMessageBody(){
        byte[] commandCount = ByteUtils.short2byte((short)1);
        byte[] systemNo=ByteUtils.short2byte((short)2);
        byte[] functionNo=ByteUtils.int2byte(330403);
        byte[] params = "request_num=5".getBytes();
        byte[] paramsLen = ByteUtils.short2byte((short)params.length);
        byte[] body=new byte[commandCount.length+systemNo.length+functionNo.length+paramsLen.length+params.length+1];
        int destPos=0;
        System.arraycopy(commandCount,0,body,destPos,commandCount.length);
        destPos=destPos+commandCount.length;
        System.arraycopy(systemNo,0,body,destPos,systemNo.length);
        destPos=destPos+systemNo.length;
        System.arraycopy(functionNo,0,body,destPos,functionNo.length);
        destPos=destPos+functionNo.length;
        System.arraycopy(paramsLen,0,body,destPos,paramsLen.length);
        destPos=destPos+paramsLen.length;
        System.arraycopy(params,0,body,destPos,params.length);
	       //endwith \0
         body[body.length-1]='\0';
        
		 ByteUtils.println("commandCount",commandCount);
		 ByteUtils.println("systemNo",systemNo);
		 ByteUtils.println("functionNo",functionNo);
		 ByteUtils.println("params",params);
		 ByteUtils.println("paramsLen",paramsLen);
		 ByteUtils.println("body",body);
        return body;
	}
	
	
}
